package com.xhwl.centiotdata.handler.eesd.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.eesd.EesdFactory;
import com.xhwl.centiotdata.handler.eesd.EesdHandler;
import com.xhwl.centiotdata.pojo.dto.eesd.EesdConfigDTO;
import com.xhwl.centiotdata.pojo.enums.eesd.EesdMethodEnum;
import com.xhwl.centiotdata.service.cent.IBiEnvCentService;
import com.xhwl.centiotdata.service.cent.ICentAlarmService;
import com.xhwl.centiotdata.suppliersystem.eesd.renke.RenkeApiHandler;
import com.xhwl.common.pojo.cent.bi.DwdFactEnvironmentMetrics;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.utils.ChkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author： 仁科环境监测
 * @Description：
 * @Date 2021/09/06/ 11:31
 */
@Service
@Slf4j
public class EesdRenkeHandler extends EesdHandler {
    @Autowired
    private RenkeApiHandler renkeApiHandler;

    @Resource
    private IBiEnvCentService biEnvCentService;

    @Autowired
    private ICentAlarmService iCentAlarmService;

    @Override
    public void afterPropertiesSet() {
        EesdHandler invokeStrategy = EesdFactory.getInvokeStrategy(EesdMethodEnum.RENKE.getId());
        if(invokeStrategy == null){
            EesdFactory.register(EesdMethodEnum.RENKE.getId(), this);
        }
    }

    @Override
    public void pullRecord(EesdConfigDTO eesdConfigDTO) {
        renkeApiHandler.pullRecord(eesdConfigDTO);
    }

    @Override
    public void pullEnvTenMinusData(EesdConfigDTO config) {
        renkeApiHandler.pullRecordAndStorage(config);
    }

    /**
     * 获取实时数据，并更新入库，判断是否有告警 ,十分钟一次
     * 调用位置:com.xhwl.centiotdata.pollingtrigger.eesd.EesdDispatcher#executeEnvOriginalData()
     *
     * @param config
     */
    @Override
    public void pullEnvData(DeviceProjSupplierCategory config) {
        try {
            List<DwdFactEnvironmentMetrics> list = renkeApiHandler.pullEnvDevice(config);
            if (CollectionUtils.isEmpty(list)) {
                log.error("仁科拉取环境实时数据到设备中台 失败： 数据为空");
                return;
            }
            String dataStr = JSONObject.toJSONString(list);
//            biEnvCentService.pushEnvRealTimeData(dataStr);
            log.info("仁科拉取环境实时数据 结束，" + dataStr);

            iCentAlarmService.iotEnvironmentReceive(dataStr);
        } catch (Exception e) {
            log.error("仁科拉取环境实时数据到设备中台 失败： {}", e.getMessage());
        }
    }
}
